<template>
  <div class="main-con">
    <IndexTop></IndexTop>
    <div class="main-con-center">
      <div class="load-pages">
        <img src="@/assets/config_images/phone.png" alt />
      </div>
      <div class="push-page">
        <h4>基本设置</h4>
        <el-form ref="form" :model="form" class="push-page-top">
          <el-input v-model="form.name" placeholder="应用名称"></el-input>
          <el-input
            placeholder="应用描述"
            type="textarea"
            v-model="form.des"
            rows="5"
            style="margin-top:20px"
          ></el-input>
        </el-form>
        <div class="push-page-imgs">
          <div class="push-page-imgs-items">
            <img :src="form.cover" alt />
            <div @click="showImg = true, setImg('cover')">更换封面</div>
          </div>
          <div class="push-page-imgs-items">
            <img :src="form.logo" alt />
            <div @click="showImg = true, setImg('logo')">更换Logo</div>
          </div>
        </div>
        <div class="group-btn-con">
          <div class="group-btn-item" @click="goBack">
            <span>取消</span>
          </div>
          <div class="group-btn-item" @click="pushMini">
            <span>确定</span>
          </div>
        </div>
      </div>
    </div>
    <UploadImages @closeModel="closeModel" :dialogVisible="showImg"></UploadImages>
  </div>
</template>
<script>
// 导入所有Diy组件
import IndexTop from "@/components/IndexCon/userindextop";
import UploadImages from "@/components/UploadImages/UploadImages";
import { pushApplyMini, savePageJSON, editApply } from "@/api/apply";
const path = require('path')
const files = require.context('@/views/diyview/diycomponents', false, /\.vue$/)
const modules = {}
files.keys().forEach(key => {
  const name = path.basename(key, '.vue')
  modules[name] = files(key).default || files(key)
})
modules.IndexTop = IndexTop;
modules.UploadImages = UploadImages;
export default {
  components: modules,
  data() {
    return {
      showImg: false,
      coverId: "",
      logoId: "",
      form: {
        name: "",
        des: "",
        cover: "",
        logo: ""
      }
    };
  },
  mounted() {
    // 设置应用类别
  },
  methods: {
    // 判断修改还是创建
    checkStatus() {
      if (this.$store.state.apply.ApplyID) {
        this.changCon();
      } else {
        this.pushMini();
      }
    },
    closeModel() {
      const dataCon = this.$store.state.imgs;
      const type = dataCon.type;
      if (type === "cover") {
        this.form.cover = dataCon.imgUrl;
        this.coverId = dataCon.imgId;
      } else {
        this.form.logo = dataCon.imgUrl;
        this.logoId = dataCon.imgId;
      }
      this.showImg = false;
    },
    // 设置图片
    setImg(str) {
      this.$store.commit("imgs/SET_TYPE", str);
    },
    // 返回上一页
    goBack() {
      this.$router.go(-1);
    },
    // 发布小程序
    pushMini() {
      const { name, cover, logo, des } = this.form;
      pushApplyMini(1, name, cover, logo, des).then(res => {
        if (res.code === 200) {
          this.$message.success(res.msg);
          // 保存页面
          this.savePage(res.data.app_id);
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    savePage(applyid) {
      savePageJSON(applyid, this.$store.state.apply.AllPageJSON).then(res => {
        if (res.code === 200) {
          this.$message.success(res.msg);
        }
      });
    },
    // 修改内容
    changCon() {
      editApply(
        this.$store.state.apply.ApplyID,
        this.form.name,
        this.form.cover
      ).then(res => {
        if (res.code === 200) {
          this.savePagethis.$store.state.apply.ApplyID();
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.main-con {
  width: 100%;
  height: 100%;
  position: absolute;
  overflow: hidden;
  background: rgba(240, 246, 254, 1);
  .main-con-center {
    width: 50%;
    margin: 200px auto;
    display: flex;
    justify-content: space-between;
    .load-pages {
      background-image: url(/src/assets/config_images/phone.png);
    }
    .push-page {
      width: 400px;
      height: 500px;
      background: rgba(255, 255, 255, 1);
      box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.2);
      border-radius: 3px;
      margin-top: 50px;
      h4 {
        height: 50px;
        line-height: 50px;
        text-align: center;
        color: #333333;
        border-bottom: 1px solid #dfdfdf;
      }
      .push-page-top {
        width: 100%;
        padding: 0 35px;
      }
      .group-btn-con {
        width: 100%;
        height: 86px;
        align-items: center;
        padding: 21px 86px;
        display: flex;
        border-top: 1px solid #dfdfdf;
        justify-content: space-between;
        margin-top: 12px;
        .group-btn-item {
          width: 100px;
          height: 40px;
          line-height: 40px;
          color: #a2a2a2;
          font-size: 14px;
          background: rgba(245, 245, 245, 1);
          border-radius: 2px;
          text-align: center;
          cursor: pointer;
        }
        .group-btn-item:nth-child(2) {
          background: #1f75f6;
          color: #ffffff;
        }
      }
      .push-page-imgs {
        width: 100%;
        padding: 0 60px;
        margin-top: 20px;
        display: flex;
        justify-content: space-between;

        .push-page-imgs-items {
          width: 120px;
          height: 120px;
          position: relative;
          div {
            position: absolute;
            left: 0;
            bottom: 0;
            width: 120px;
            height: 30px;
            background: rgba(0, 0, 0, 1);
            opacity: 0.5;
            z-index: 100;
            color: #ffffff;
            text-align: center;
            line-height: 30px;
            font-size: 14px;
            cursor: pointer;
          }
          img {
            width: 100%;
            height: 100%;
          }
        }
      }
    }
  }
}
</style>
